package com.elangzhi.ssm.modules.message.app;

import com.elangzhi.ssm.controller.json.Tip;
import com.elangzhi.ssm.model.Message;
import com.elangzhi.ssm.model.Record;
import com.elangzhi.ssm.model.Talk;
import com.elangzhi.ssm.modules.image.services.ImageService;
import com.elangzhi.ssm.modules.message.services.MessageService;
import com.elangzhi.ssm.modules.record.services.RecordService;
import com.elangzhi.ssm.modules.talk.services.TalkService;
import com.elangzhi.ssm.modules.user.services.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
* 消息 客户端 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/app/message")
public class AppMessageController {

    /**
     * 设置全部未读为已读
     * @param userId 用户id
     * @return 成功状态
     */
    @RequestMapping("/setAllRead")
    @ResponseBody
    public Tip setAllRead(Long userId){
        messageService.setAllRead(userId);
        return new Tip();
    }

    /**
     * 获取我的消息列表
     * @param userId  用户id
     * @param page 第几页
     * @param size 每页大小
     * @return 列表
     */
    @RequestMapping("/listByUserId")
    @ResponseBody
    public PageInfo<Message> listByUserId(Long userId,
                                          @RequestParam Integer page,
                                          @RequestParam Integer size){
        PageInfo<Message> messagePageInfo = messageService.listByUserId(userId,page,size);
        for(Message message : messagePageInfo.getList()){

            message.setSendUser(userService.selectById(message.getSend()));

            if(message.getType() == 0 || message.getType() == 1){

                Talk talk = talkService.selectById(message.getInfoId());

                if(talk != null){
                    talk.setUser(userService.selectById(talk.getUserId()));
                    talk.setBannerList(imageService.listByTypeInfo(4,talk.getId()));
                    message.setTalk(talk);
                }

            }else{

                Record record = recordService.selectById(message.getInfoId());
                message.setRecord(record);

                if(record == null){ continue; }
                Talk talk = talkService.selectById(record.getInfoId());
                if(talk != null){
                    talk.setUser(userService.selectById(talk.getUserId()));
                    talk.setBannerList(imageService.listByTypeInfo(4,talk.getId()));
                    message.setTalk(talk);
                }


            }
        }
        return messagePageInfo;
    }

    /**
     * 获取用户未读数量
     * @param userId 用户id
     * @return 数量
     */
    @RequestMapping("/listByUnRead")
    @ResponseBody
    public Integer listByUnRead(Long userId){
        return messageService.countByUnRead(userId);
    }





    //---------------------------- property -------------------------------

    @Resource
    private MessageService messageService;

    @Resource
    private TalkService talkService;

    @Resource
    private RecordService recordService;

    @Resource
    private UserService userService;

    @Resource
    private ImageService imageService;

}
